<%@ page import="org.springframework.util.ClassUtils" %>
<%@ page import="org.codehaus.groovy.grails.plugins.searchable.SearchableUtils" %>
<%@ page import="org.codehaus.groovy.grails.plugins.searchable.lucene.LuceneUtils" %>
<%@ page import="org.codehaus.groovy.grails.plugins.searchable.util.StringQueryUtils" %>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta name="layout" content="main" />
    
        <link rel="stylesheet" href="${resource(dir:'css/jquery/plugins',file:'ui.jqgrid.css')}" />
        <script type="text/javascript" src="${resource(dir:'js/jquery/plugins',file:'grid.locale-en.js')}"></script>
        <script type="text/javascript" src="${resource(dir:'js/jquery/plugins',file:'jquery.jqGrid.min.js')}"></script>
        
  </head>
  <body>
  <div id="main">
    <g:set var="haveQuery" value="${params.q?.trim()}" />
    <g:set var="haveResults" value="${searchResult?.results}" />
    <div class="title">
      <span>
        <g:if test="${haveQuery && haveResults}">
          
			<div style="margin-top: 2px; padding: 0pt 0.7em; padding:4px;" class="ui-state-highlight ui-corner-all"> 
				<span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-info"></span>
				Foram encontrados <strong>${searchResult.total}</strong> ocorrencias para <strong>${params.q}</strong>
			</div>
        </g:if>
        <g:else>
        &nbsp;
        </g:else>
      </span>
    </div>

    <g:if test="${haveQuery && !haveResults && !parseException}">
      <p>Nothing matched your query - <strong>${params.q}</strong></p>
      <g:if test="${!searchResult?.suggestedQuery}">
        <p>Suggestions:</p>
        <ul>
          <li>Try a suggested query: <g:link controller="localizador" action="index" params="[q: params.q, suggestQuery: true]">Search again with the <strong>suggestQuery</strong> option</g:link><br />
            <em>Note: Suggestions are only available when classes are mapped with <strong>spellCheck</strong> options, either at the class or property level.<br />
		The simplest way to do this is add <strong>spellCheck "include"</strong> to the domain class searchable mapping closure.<br />
                See the plugin/Compass documentation Mapping sections for details.</em>
          </li>
        </ul>
      </g:if>
    </g:if>

    <g:if test="${searchResult?.suggestedQuery}">
      <p>Did you mean <g:link controller="localizador" action="index" params="[q: searchResult.suggestedQuery]">${StringQueryUtils.highlightTermDiffs(params.q.trim(), searchResult.suggestedQuery)}</g:link>?</p>
    </g:if>

    <g:if test="${parseException}">
      <p>Your query - <strong>${params.q}</strong> - is not valid.</p>
      <p>Suggestions:</p>
      <ul>
        <li>Fix the query: see <a href="http://lucene.apache.org/java/docs/queryparsersyntax.html">Lucene query syntax</a> for examples</li>
        <g:if test="${LuceneUtils.queryHasSpecialCharacters(params.q)}">
          <li>Remove special characters like <strong>" - [ ]</strong>, before searching, eg, <em><strong>${LuceneUtils.cleanQuery(params.q)}</strong></em><br />
              <em>Use the Searchable Plugin's <strong>LuceneUtils#cleanQuery</strong> helper method for this: <g:link controller="localizador" action="index" params="[q: LuceneUtils.cleanQuery(params.q)]">Search again with special characters removed</g:link></em>
          </li>
          <li>Escape special characters like <strong>" - [ ]</strong> with <strong>\</strong>, eg, <em><strong>${LuceneUtils.escapeQuery(params.q)}</strong></em><br />
              <em>Use the Searchable Plugin's <strong>LuceneUtils#escapeQuery</strong> helper method for this: <g:link controller="localizador" action="index" params="[q: LuceneUtils.escapeQuery(params.q)]">Search again with special characters escaped</g:link></em><br />
              <em>Or use the Searchable Plugin's <strong>escape</strong> option: <g:link controller="localizador" action="index" params="[q: params.q, escape: true]">Search again with the <strong>escape</strong> option enabled</g:link></em>
          </li>
        </g:if>
      </ul>
    </g:if>

    <g:if test="${haveResults}">
      <div class="results">
        <g:each var="result" in="${searchResult.results}" status="index">
          <div class="result" style="padding:5px;">
            <g:set var="className" value="${ClassUtils.getShortName(result.getClass())}" />
            <g:set var="link" value="${createLink(controller: className[0].toLowerCase() + className[1..-1], action: 'show', id: result.id)}" />
            <g:set var="desc" value="${result.toString()}" />
            
            <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active"  style="padding: 5px;">
            	<div class="desc">
            		${className} - ${desc.encodeAsHTML()}<br/> 
            		
            		<div class="inline">
            			Link: <a href="${link}">${link}</a>
            		</div>
			  		<div class="ui-state-default ui-corner-all inline">
				  		<span class="ui-icon ui-icon-star"></span>
			  		</div>
			  		<div class="ui-state-default ui-corner-all inline">
				  		<span class="ui-icon ui-icon-pencil"></span>
			  		</div>
			  		<div class="ui-state-default ui-corner-all inline">
				  		<span class="ui-icon ui-icon-folder-open"></span>
			  		</div>
			  		
					<br class="clearBoth" />
					
            	</div>
            </div>
            
          </div>
        </g:each>
      </div>

      <div>
        <div class="paging">
          <g:if test="${haveResults}">
              Page:
              <g:set var="totalPages" value="${Math.ceil(searchResult.total / searchResult.max)}" />
              <g:if test="${totalPages == 1}"><span class="currentStep">1</span></g:if>
              <g:else><g:paginate controller="localizador" action="index" params="[q: params.q]" total="${searchResult.total}" prev="&lt; previous" next="next &gt;"/></g:else>
          </g:if>
        </div>
      </div>
    </g:if>
  </div>
  
  
<%--
  
  
  
  Teste grid
<table id="list4"></table>
<div id="pager2"></div>

  <script type="text/javascript">
  jQuery("#list4").jqGrid({ 
	  datatype: "local", 
	  height: 250, 
	  colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'], 
	  colModel:[ 	{name:'id',index:'id', width:60, sorttype:"int"}, 
	       	  		{name:'invdate',index:'invdate', width:90, sorttype:"date"}, 
	       	  		{name:'name',index:'name', width:100}, 
	       	  		{name:'amount',index:'amount', width:80, align:"right",sorttype:"float"}, 
	       	  		{name:'tax',index:'tax', width:80, align:"right",sorttype:"float"}, 
	       	  		{name:'total',index:'total', width:80,align:"right",sorttype:"float"}, 
	       	  		{name:'note',index:'note', width:150, sortable:false} 
	       	  	], 
		rowNum:10, 
		rowList:[10,20,30], 
		pager: '#pager2',
		caption: "Manipulating Array Data" }); 

  	var mydata = [ 
  	          	{id:"1",invdate:"2007-10-01",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"}, 
  	          	{id:"2",invdate:"2007-10-02",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"}, 
  	          	{id:"3",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"}, 
  	          	{id:"4",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"}, 
  	          	{id:"5",invdate:"2007-10-05",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"}, 
  	          	{id:"6",invdate:"2007-09-06",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"}, 
  	          	{id:"7",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"}, 
  	          	{id:"8",invdate:"2007-10-03",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"}, 
  	          	{id:"9",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"}, 
  	          	{id:"10",invdate:"2007-10-01",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"}, 
  	          	{id:"11",invdate:"2007-10-02",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"}, 
  	          	{id:"12",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"}, 
  	          	{id:"13",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"}, 
  	          	{id:"14",invdate:"2007-10-05",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"}, 
  	          	{id:"15",invdate:"2007-09-06",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"}, 
  	          	{id:"16",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"}, 
  	          	{id:"17",invdate:"2007-10-03",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"}, 
  	          	{id:"18",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"} 
  	          ]; 


    for(var i=0;i<=mydata.length;i++){
        jQuery("#list4").jqGrid('addRowData',i+1,mydata[i]);
    } 
  </script>
 --%>
  </body>
</html>
